/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/5/15 20:27
 */
// 区分最长重复子数组
public class Solution1143 {
    public int longestCommonSubsequence(String text1, String text2) {
        // 转成字符数组处理
        char[] chr1 = text1.toCharArray();
        char[] chr2 = text2.toCharArray();
        // 初始化dp数组
        int[][] dp = new int[text1.length() + 1][text2.length() + 1];

        for (int i = 1; i <= text1.length(); i++) {
            for (int j = 1; j <= text2.length(); j++) {
                // 如果字符相等，长度加一，从dp[i - 1][j - 1]转移
                if (chr1[i-1] == chr2[j-1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                // 如果不相等
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j- 1]);
                }
            }
        }
        return dp[text1.length()][text2.length()];
    }
}
